/**
 * Created by liubin on 2017/6/2.
 */


var DtjCardLayer = cc.Layer.extend({

    mDtjTableDiceLayer : null,
    mCardLayer : null,
    mCardss : null,
    mMyCards : null,
    mSz : null,
    mSelectedCards : null,
    hasOpenCard : null,

    mFirstSeat : null,

    mCardTypeSpr1 : null,
    mCardTypeSpr2 : null,

    //
    mCanMove : null,
    mMovedOutFlag : null, //牌是否移出范围
    mTouchCardInitPoint : null,
    mTouchCardLastPoint : null,

    otherCardNums : null,

    ctor: function () {
        this._super();

        //card
        this.mCardLayer = new cc.Layer();
        this.addChild(this.mCardLayer);

        this.mCardss = [];
        for (var i=0; i<32; i++){
            var card = new DtjCard();
            // card.setVisible(false);
            card.setTag(i);
            card.setScale(mo.dtjCardHelper.deskCardScale);
            this.mCardLayer.addChild(card);

            var pos = cc.p(mo.gameSize.x/2, mo.gameSize.y/2);

            var cardSize = mo.dtjCardHelper.getDeskCardSize();
            var x = pos.x - 3.5 * cardSize.width + i % 8 * cardSize.width;
            var y = pos.y + 1.5 * cardSize.height - parseInt(i / 8) * cardSize.height;
            card.setPosition(cc.p(x, y));

            this.mCardss.push(card);
        }

        this.mDtjTableDiceLayer = new DtjTableDiceLayer();
        this.addChild(this.mDtjTableDiceLayer);
        this.mDtjTableDiceLayer.setVisible(false);

        this.mSz = [];
        for (var i=0; i<2; i++){
            var sz = new DtjSz();
            sz.setVisible(false);
            sz.setTag(i);
            this.mCardLayer.addChild(sz);

            var pos = cc.p(mo.gameSize.x/2, mo.gameSize.y/2);
            var szSize = mo.dtjCardHelper.getSzSize();

            var x = pos.x + (i - 0.5) * szSize.width;
            var y = pos.y;
            sz.setPosition(cc.p(x, y));

            this.mSz.push(sz);
        }

        this.otherCardNums = [];
        this.mMyCards = [];
        this.mSelectedCards = [];

        this.loadMyInhandCardAction();

    },

    initCardTypeTips : function () {
        this.mCardTypeSpr1 = new cc.Sprite();
        this.mCardTypeSpr2 = new cc.Sprite();

        this.mCardTypeSpr1.setVisible(false);
        this.mCardTypeSpr2.setVisible(false);

        this.mCardTypeSpr1.setScale(0.8);
        this.mCardTypeSpr2.setScale(0.8);

        this.mCardTypeSpr1.setAnchorPoint(cc.p(0, 0));
        this.mCardTypeSpr2.setAnchorPoint(cc.p(0, 1));

        this.addChild(this.mCardTypeSpr1);
        this.addChild(this.mCardTypeSpr2);

        var cardPos = mo.dtjCardHelper.getInhandCardPos(0);
        var cardSize = mo.dtjCardHelper.getInhandCardSize();

        this.mCardTypeSpr1.setPosition(cc.p(cardPos.x + cardSize.width * 2 + 10, cardPos.y + 10));
        this.mCardTypeSpr2.setPosition(cc.p(cardPos.x + cardSize.width * 2 + 10, cardPos.y - 10));
    },

    reset : function () {
        this.mMyCards = [];
        this.mSelectedCards = [];
        this.hasOpenCard = false;
        this.mCanMove = false;
        this.otherCardNums = [];

        for (var i=0; i<32; i++){
            var card = this.mCardss[i];
            card.setScale(mo.dtjCardHelper.deskCardScale);
            card.setCardNum(0);
            card.setRotation(0);
            card.setSpriteFrame("card_back.png");
            card.mNumSpr.setPosition(cc.p(60 * 0.5 - 3, 133 * 0.5));

            var pos = cc.p(mo.gameSize.x/2, mo.gameSize.y/2);

            var cardSize = mo.dtjCardHelper.getDeskCardSize();
            var x = pos.x - 3.5 * cardSize.width + i % 8 * cardSize.width;
            var y = pos.y + 1.5 * cardSize.height - parseInt(i / 8) * cardSize.height;
            card.setPosition(cc.p(x, y));
        }
    },

    yaoSz : function (nums, firstSeat) {
        // for (var i=0; i<2; i++){
        //     this.mSz[i].setVisible(true);
        //     this.mSz[i].yaoSz(nums[i]);
        // }

        var delayTime = this.mDtjTableDiceLayer.showDiceAnim(nums, firstSeat);
        return delayTime;
    },

    setUsedCards : function (usedCards) {

        if(usedCards.length > 32){
            return;
        }

        for(var i=0; i<usedCards.length; i++){
            var card = this.mCardss[32 - usedCards.length + i];
            var cardNum = usedCards[i];

            card.setCardNum(cardNum);
            card.setCardSprite(cardNum);
        }
    },

    //发牌
    fapai : function (cardNums, firstSeat) {

        var handCardLength = 4;
        if(mo.dtjTable.mGameConfig.paijiu == 1){
            handCardLength = 2;
        }

        this.mFirstSeat = firstSeat;
        var playerAni = true;
        if(mo.dtjTable.mPlayerType == 0){
            playerAni = false;
        }
        for (var i=0; i<4; i++){
            var seat = (i+firstSeat) % 4;
            var cardPos = mo.dtjCardHelper.getInhandCardPos(seat);

            for (var j=0; j<handCardLength; j++){
                var card = this.mCardss[i*handCardLength+j];
                card.setTag(-1);
                var cardSize = mo.dtjCardHelper.getDeskCardSize();

                var targetScale = mo.dtjCardHelper.cardScale;
                if(seat == 0){
                    card.setCardNum(cardNums[seat][j]);
                    if(handCardLength == 2 && j == 1){
                        card.setCardSprite(cardNums[seat][j]);
                    }

                    targetScale = mo.dtjCardHelper.myInHandCardScale;
                    card.setTag(j);
                    this.mMyCards.push(card);
                }

                var nowScale = card.getScale();
                cardSize.width = cardSize.width / nowScale * targetScale;

                var targetX = cardPos.x - (handCardLength / 2 - 0.5) * cardSize.width + j % handCardLength * cardSize.width;
                var targetY = cardPos.y;

                if(handCardLength == 2 && seat == 0){
                    targetX = cardPos.x - j * 6 * targetScale;
                    targetY = cardPos.y - j * 6 * targetScale;
                }

                var that = this;
                if(playerAni){
                    card.runAction(cc.sequence(
                        cc.delayTime(0.05*(i*handCardLength+j)),
                        cc.callFunc(function(){
                            mo.soundHelper.playDtjSendCard();
                        }),
                        cc.spawn(cc.moveTo(0.5, cc.p(targetX, targetY)), cc.scaleTo(0.5, targetScale)),
                        cc.delayTime(0.5),
                        cc.callFunc(function(){
                            if(handCardLength == 4){
                                this.fanpai();
                            }else{
                                var fisrtCardNum = that.mMyCards[0].mCardNum;
                                that.mMyCards[0].setCardSprite(fisrtCardNum);
                            }
                        }, card)
                        )
                    );
                }else{
                    if(seat == 0){
                        card.setCardSprite(card.mCardNum);
                    }
                    card.setScale(targetScale);
                    card.setPosition(cc.p(targetX, targetY));
                }
            }
        }

        var that = this;
        for (var i=handCardLength*4; i<32; i++){
            var card = this.mCardss[i];
            var cardSize = mo.dtjCardHelper.getDeskCardSize();
            if(playerAni){
                card.runAction(cc.sequence(
                    cc.delayTime(0.05 * 16 + 0.5),
                    cc.moveBy(0.2, cc.p(0, cardSize.height * handCardLength / 4)),
                    cc.delayTime(0.5),
                    cc.callFunc(function(){
                        // this.fanpai();
                        mo.dtjTable.mDtjTableUILayer.showOprationLayer();
                        that.mCanMove = true;
                    }, card)
                ));
            }else{
                var cardSize = mo.dtjCardHelper.getCardSize();
                card.setPosition(cc.pAdd(card.getPosition(), cc.p(0, cardSize.height * handCardLength / 4)));
            }
        }
    },

    setInhandCard : function (cardNums) {

        var handCardLength = 4;
        if(mo.dtjTable.mGameConfig.paijiu == 1){
            handCardLength = 2;
        }

        for (var i=0; i<handCardLength * 4; i++){
            var card = this.mCardss[i];

            var seat = parseInt(i / handCardLength);
            var cardPos = mo.dtjCardHelper.getInhandCardPos(seat);

            var cardSize = mo.dtjCardHelper.getDeskCardSize();

            var targetScale = mo.dtjCardHelper.cardScale;
            if(seat == 0){
                targetScale = mo.dtjCardHelper.myInHandCardScale;
                card.setTag(i % handCardLength);
                this.mMyCards.push(card);
            }

            if(cardNums[seat].length == handCardLength){
                var cardNum = cardNums[seat][i % handCardLength];
                if(cardNum != 0){
                    card.setCardNum(cardNum);
                    if(handCardLength == 4){
                        card.setCardSprite(card.mCardNum);
                    }else if(seat == 0){
                        card.setCardSprite(card.mCardNum);
                    }
                }
            }

            var nowScale = card.getScale();
            cardSize.width = cardSize.width / nowScale * targetScale;

            var targetX = cardPos.x - (handCardLength / 2 - 0.5) * cardSize.width + i % handCardLength * cardSize.width;
            var targetY = cardPos.y;

            if(handCardLength == 2 && seat == 0){
                targetX = cardPos.x - i % handCardLength * 6 * targetScale;
                targetY = cardPos.y - i % handCardLength * 6 * targetScale;
            }

            card.setScale(targetScale);
            card.setPosition(cc.p(targetX, targetY));

        }

        for (var i=handCardLength*4; i<32; i++){
            var card = this.mCardss[i];
            var cardSize = mo.dtjCardHelper.getDeskCardSize();

            card.setPosition(cc.pAdd(card.getPosition(), cc.p(0, cardSize.height * handCardLength / 4)));
        }
    },

    setSelectedState : function (selectedState, cardNums) {
        var handCardLength = 4;
        if(mo.dtjTable.mGameConfig.paijiu == 1){
            handCardLength = 2;
        }

        for (var seat=0; seat<4; seat++){
            var hasSelected = selectedState[seat];

            if(hasSelected){

                var cardPos = mo.dtjCardHelper.getInhandCardPos(seat);
                var cardSize = mo.dtjCardHelper.getCardSize();

                for (var i=handCardLength-1; i>=0; i--){
                    var card = this.mCardss[seat*handCardLength + i];

                    if(cardNums[seat].length == handCardLength){
                        var cardNum = cardNums[seat][i];
                        card.setCardSprite(cardNum);
                        // card.setSpriteFrame("card"+cardNum+".png");
                    }

                    card.setScale(mo.dtjCardHelper.cardScale);

                    var targetX = cardPos.x - (handCardLength / 2 - 0.5) * cardSize.width + i % handCardLength * cardSize.width;
                    var targetY = cardPos.y;
                    if(i >= 2){
                        card.mSelected = true;
                        card.setDtjCardRotation(90);
                        targetX = cardPos.x + 0.5 * cardSize.height;
                        targetY = cardPos.y - 0.5 * cardSize.width + cardSize.width * (i % 2);
                    }
                    card.setPosition(cc.p(targetX, targetY));
                }

                if(cardNums[seat].length == handCardLength){
                    mo.dtjTable.mDtjTableUILayer.showOpenCardType(seat, cardNums[seat]);
                }

            }else{
                if(seat == 0){
                    this.mCanMove = true;
                    mo.dtjTable.mDtjTableUILayer.showOprationLayer();
                }
            }
        }
    },

    //自己的手牌操作
    loadMyInhandCardAction : function(){
        var that = this;
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: function(touch, event){
                if(that.hasOpenCard){
                    return;
                }

                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);

                if(mo.dtjTable.mGameConfig.paijiu == 1){
                    return that.onTouchCardBegan(point);
                }

                var cards = that.mMyCards;
                for (var i = 0; i < cards.length; i++){
                    var card = cards[i];
                    if (cc.rectContainsPoint(card.getBoundingBox(), point)){
                        var isSelected = that.setSelectInhandCard(card);
                        //
                        // //选牌音效
                        mo.soundHelper.playDtjSelect(isSelected);
                        break;
                    }
                }
                return false;
            },
            onTouchMoved: function(touch, event){
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);
                if(mo.dtjTable.mGameConfig.paijiu == 1 && that.mMovedOutFlag == false){
                    that.mMovedOutFlag = !that.onTouchCardMove(point);
                }
            },
            onTouchEnded: function(touch, event){
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);
                if(mo.dtjTable.mGameConfig.paijiu == 1 && that.mMovedOutFlag == false){
                    return that.onTouchCardEnd(point);
                }
            },
            onTouchCancelled : function(touch, event){

            }
        }, this);
    },

    onTouchCardBegan : function (point) {

        if(!this.mCanMove){
            return false;
        }

        this.mMovedOutFlag = false;
        var card = this.mMyCards[1];
        if (cc.rectContainsPoint(card.getBoundingBox(), point)){
            this.mTouchCardLastPoint = point;
            this.mTouchCardInitPoint = card.getPosition();
            mo.soundHelper.playDtjSelect(true);
            return true;
        }

        return false;
    },

    onTouchCardMove : function (point) {

        var card0 = this.mMyCards[0];
        var card1 = this.mMyCards[1];

        var del_pos = cc.pSub(point, this.mTouchCardLastPoint);
        card1.setPosition(cc.pAdd(card1.getPosition(), del_pos));
        this.mTouchCardLastPoint = point;

        var card1_rect = cc.rect(card1.getBoundingBox().x - 40, card1.getBoundingBox().y - 70,
            card1.getBoundingBox().width + 40, card1.getBoundingBox().height + 70);


        if(cc.rectIntersectsRect(card0.getBoundingBox(), card1_rect)){
            return true;
        }

        card1.setPosition(this.mTouchCardInitPoint);
        mo.dtjTable.mDtjTableUILayer.openMyCard();

        // this.openMyCard();
        return false;
    },

    onTouchCardEnd : function (point) {
        var card = this.mMyCards[1];
        card.setPosition(this.mTouchCardInitPoint);
        mo.soundHelper.playDtjSelect(false);
        return true;
    },

    setMyCardPos : function () {
        var cardPos = mo.dtjCardHelper.getInhandCardPos(0);
        for (var i=0; i<2; i++){
            var card = this.mMyCards[i];
            card.setPosition(cardPos);
            card.stopAllActions();

            var cardSize = card.getContentSize();
            cardSize.width = 60 * 1.2;

            var targetX = cardPos.x - (2 / 2 - 0.5) * cardSize.width + i % 2 * cardSize.width;
            var targetY = cardPos.y;

            card.runAction(cc.moveTo(0.2, cc.p(targetX, targetY)));
        }
    },

    setSelectInhandCard : function (card) {

        var hasSelected = false;

        for (var i=0 ; i<this.mSelectedCards.length; i++){
            var selectedCard = this.mSelectedCards[i];
            if(card.getTag() == selectedCard.getTag()){
                this.mSelectedCards.splice(i, 1);
                hasSelected = true;
                break;
            }
        }

        if(hasSelected == false){
            this.mSelectedCards.push(card);

            if(this.mSelectedCards.length > 2){
                this.mSelectedCards.splice(0, 1);
            }
        }

        this.updateInhandCardPosition();
        this.updateOpenBtnState();

        return !hasSelected;
    },

    updateInhandCardPosition : function () {
        var inHandCardPos = mo.dtjCardHelper.getInhandCardPos(0);

        for (var i=0; i<4; i++){
            var card = this.mMyCards[i];
            card.setPosition(cc.p(card.getPositionX(), inHandCardPos.y));
        }

        for (var i=0; i<this.mSelectedCards.length; i++){
            var card = this.mSelectedCards[i];
            card.setPosition(cc.p(card.getPositionX(), inHandCardPos.y + 30));
        }
    },

    updateOpenBtnState : function () {

        var canOpen = (this.mSelectedCards.length == 2);
        mo.dtjTable.mDtjTableUILayer.openBtn.setBright(canOpen);
        mo.dtjTable.mDtjTableUILayer.openBtn.setTouchEnabled(canOpen);

        if(canOpen){
            this.mCardTypeSpr1.setVisible(true);
            this.mCardTypeSpr2.setVisible(true);

            var unSelectedCards = [];
            for (var j=0; j<2; j++){
                var cardNum = this.mSelectedCards[j].mCardNum;
                for (var i=0; i<4; i++){
                    var card = this.mMyCards[i];
                    if(card.mCardNum == cardNum){
                        var tmpCard = this.mMyCards.splice(i, 1);
                        this.mMyCards.push(tmpCard[0]);
                        break;
                    }
                }
            }

            this.mSelectedCards = [];

            unSelectedCards.push(this.mMyCards[0]);
            unSelectedCards.push(this.mMyCards[1]);

            this.mSelectedCards.push(this.mMyCards[2]);
            this.mSelectedCards.push(this.mMyCards[3]);

            var num1 = this.mSelectedCards[0].mCardNum;
            var num2 = this.mSelectedCards[1].mCardNum;
            var num3 = unSelectedCards[0].mCardNum;
            var num4 = unSelectedCards[1].mCardNum;

            var cardTypeIndex_1 = mo.dtjCardHelper.getCardGroup(num1, num2);
            var cardTypeIndex_2 = mo.dtjCardHelper.getCardGroup(num3, num4);

            this.mCardTypeSpr1.setSpriteFrame("cardType" + (cardTypeIndex_1 + 1) + ".png");
            this.mCardTypeSpr2.setSpriteFrame("cardType" + (cardTypeIndex_2 + 1) + ".png");
        }else{
            this.mCardTypeSpr1.setVisible(false);
            this.mCardTypeSpr2.setVisible(false);
        }

    },


    //显示提示的牌型组合
    showTishiCardGroup : function () {

        var cardNums = [];
        for (var i=0; i<this.mMyCards.length; i++){
            cardNums.push(this.mMyCards[i].mCardNum);
        }

        var tishiCardGroup = mo.dtjCardHelper.getTishiCardGroup(cardNums);

        this.mSelectedCards = [];

        for (var i=0; i<2; i++){
            var cardNum = tishiCardGroup[i];
            for (var j=0; j<this.mMyCards.length; j++){
                if(cardNum == this.mMyCards[j].mCardNum){
                    var card = this.mMyCards.splice(j, 1);
                    this.mMyCards.push(card[0]);
                    break;
                }
            }
        }

        this.mSelectedCards.push(this.mMyCards[0]);
        this.mSelectedCards.push(this.mMyCards[1]);

        // for (var i=0; i<4; i++){
        //     if(this.mMyCards[i].mCardNum != tishiCardGroup[0]
        //         && this.mMyCards[i].mCardNum != tishiCardGroup[1]){
        //         this.mSelectedCards.push(this.mMyCards[i]);
        //     }
        // }


        this.updateInhandCardPosition();
        this.updateOpenBtnState();
    },


    //
    openOtherCard : function (seat, cardNums) {
        if(seat == 0){
            if(mo.dtjTable.mPlayerType == 0 || (mo.dtjTable.mPlayerType == 1 && this.hasOpenCard == false)){
                this.mSelectedCards = [];
                // for (var i=0; i<4; i++){
                //     if(this.mMyCards[i].mCardNum != cardNums[0]
                //         &&this.mMyCards[i].mCardNum != cardNums[1]){
                //         this.mSelectedCards.push(this.mMyCards[i]);
                //     }
                // }

                for (var j=0; j<2; j++){
                    var cardNum = cardNums[j];
                    for (var i=0; i<4; i++){
                        var card = this.mMyCards[i];
                        if(card.mCardNum == cardNum){
                            var tmpCard = this.mMyCards.splice(i, 1);
                            this.mSelectedCards.push(tmpCard[0]);
                            break;
                        }
                    }
                }

                this.mMyCards.push(this.mSelectedCards[0]);
                this.mMyCards.push(this.mSelectedCards[1]);

                this.openMyCard();
            }
        }else{
            this.openOtherCardStepOne(seat, cardNums);
        }

    },


    autoOpenMyCard : function (seat) {
        var tableStatus = mo.dtjTableStatus.mTableStatus;

        if(seat == 0){
            return;
        }

        var otherCardNums = this.otherCardNums;
        var myCardNums = [];
        for (var i = 0; i < 4; i++){
            myCardNums.push(this.mMyCards[i].mCardNum);
        }

        var logicSeat = mo.dtjTableStatus.getLogicForView(seat, tableStatus.MySeat - 1) + 1;
        var seatuser = tableStatus["SeatUser" + logicSeat];

        var cardNums =  seatuser.LastAction.Cards;
        var isMyHost =  tableStatus.HostSeat == tableStatus.MySeat;

        if(isMyHost){
            //自己是庄
            otherCardNums.push(cardNums);

            if(otherCardNums.length == mo.dtjTableStatus.getPlayerCount() - 1){
                var delayTime = 2 + Math.random() * 2;
                this.runAction(cc.sequence(
                    cc.delayTime(delayTime),
                    cc.callFunc(function () {
                        var myCardOrder = mo.dtjCardHelper.getMyTishiCardGroup(otherCardNums, myCardNums, true)
                        this.openMyCardByAuto(myCardOrder);
                    }, this)
                ));

            }

        }else if(logicSeat == tableStatus.HostSeat){
            //开牌的是庄
            otherCardNums.push(cardNums);

            var delayTime = Math.random() * 2;
            this.runAction(cc.sequence(
                cc.delayTime(delayTime),
                cc.callFunc(function () {
                    var myCardOrder = mo.dtjCardHelper.getMyTishiCardGroup(otherCardNums, myCardNums, false)
                    this.openMyCardByAuto(myCardOrder);
                }, this)
            ));

        }
    },

    openMyCardByAuto : function (myCardOrder) {
        if(this.hasOpenCard){
            return;
        }

        this.mSelectedCards = [];

        for (var j=0; j<2; j++){
            var cardNum = myCardOrder[j];
            for (var i=0; i<4; i++){
                var card = this.mMyCards[i];
                if(card.mCardNum == cardNum){
                    var tmpCard = this.mMyCards.splice(i, 1);
                    this.mSelectedCards.push(tmpCard[0]);
                    break;
                }
            }
        }

        this.mMyCards.push(this.mSelectedCards[0]);
        this.mMyCards.push(this.mSelectedCards[1]);

        this.openMyCard();
    },


    //牌摆成横竖
    openOtherCardStepOne : function (seat, cardNums) {

        if(mo.dtjTable.mGameConfig.paijiu == 1){
            this.openCardTwo(seat, cardNums);
            return;
        }

        this.autoOpenMyCard(seat);

        var otherCards = [];
        var cardSeat = (seat - this.mFirstSeat + 4) % 4;
        for (var i=0; i<4; i++){
            var card = this.mCardss[cardSeat * 4 + i];
            otherCards.push(card);
        }

        var selectedCards = [];
        var unSelectedCards = [];

        for (var i=0; i<4; i++){
            var randomIndex = parseInt(Math.random() * otherCards.length);
            var card = otherCards[randomIndex];
            if(i < 2){
                selectedCards.push(card);
                card.mSelected = true;
            }else{
                unSelectedCards.push(card);
                card.mSelected = false;
            }
            otherCards.splice(randomIndex, 1);
        }


        var inhandPos = mo.dtjCardHelper.getInhandCardPos(seat);
        for (var i=0; i<2; i++){
            var card = selectedCards[i];
            var cardSize = mo.dtjCardHelper.getCardSize();

            var targetX = inhandPos.x + cardSize.height / 2;
            var targetY = inhandPos.y - cardSize.width / 2 + cardSize.width * i;

            // card.setCardNum(cardNums[i]);
            card.runAction(cc.sequence(
                cc.moveTo(0.2, cc.p(targetX, targetY)),
                cc.rotateTo(0.2, 90),
                cc.callFunc(function () {
                    this.setDtjCardRotation(90);
                    // this.fanpai();
                }, card)

            ));
        }

        for (var i=0; i<2; i++){
            var card = unSelectedCards[i];
            var cardSize = mo.dtjCardHelper.getCardSize();

            var targetX = inhandPos.x - 1.5 * cardSize.width + cardSize.width * i;
            var targetY = inhandPos.y;

            // card.setCardNum(cardNums[i+2]);
            card.runAction(cc.sequence(
                cc.delayTime(0.2),
                cc.moveTo(0.2, cc.p(targetX, targetY)),
                cc.delayTime(3),
                cc.callFunc(function () {
                    // this.fanpai();
                }, card)
            ));
        }
    },

    //开牌
    openOtherCardStepTwo : function (handCards) {
        if(mo.dtjTable.mGameConfig.paijiu == 1){
            return;
        }

        var openCardSeatIndex = 0;
        for (var seat = 1; seat < 4; seat++){
            if(handCards[seat].length == 0){
                continue;
            }
            var cardSeat = (seat - this.mFirstSeat + 4) % 4;
            var cardOrder = [handCards[seat][0], handCards[seat][1], handCards[seat][2], handCards[seat][3]];

            var selectedIndex = 0;
            var unselectedIndex = 0;

            for (var i=0; i<4; i++){
                var card = this.mCardss[cardSeat * 4 + i];

                var isSelected = card.mSelected;
                if(isSelected){
                    card.setCardNum(cardOrder[selectedIndex]);
                    selectedIndex++;
                }else{
                    card.setCardNum(cardOrder[unselectedIndex + 2]);
                    unselectedIndex++;
                }

                card.runAction(cc.sequence(
                    cc.delayTime(1.5 * openCardSeatIndex),
                    cc.delayTime(isSelected ? 0 : 1.0),
                    cc.callFunc(function () {
                        this.fanpai();
                    }, card)
                ));
            }

            mo.dtjTable.mDtjTableUILayer.showCardType(seat, cardOrder, 1.5 * openCardSeatIndex);
            openCardSeatIndex++;
        }
    },

    openCardTwo : function (seat, cardNums) {
        for (var i = 0; i < 2; i++){
            var cardSeat = (seat - this.mFirstSeat + 4) % 4;

            var card = this.mCardss[cardSeat * 2 + i];
            card.setCardNum(cardNums[i]);
            card.fanpai();
        }

        var cardOrder = cardNums;
        mo.dtjTable.mDtjTableUILayer.showCardType(seat, cardOrder, 1.0);

    },

    //开牌
    openMyCard : function () {
        this.hasOpenCard = true;

        if(mo.dtjTable.mGameConfig.paijiu == 1){
            //小牌九
            this.runAction(cc.sequence(
                cc.callFunc(function(){
                    this.setMyCardPos();
                }, this),
                cc.delayTime(0.3),
                cc.callFunc(function(){
                    var cardOrder = [this.mMyCards[0].mCardNum, this.mMyCards[1].mCardNum];
                    this.openCardTwo(0, cardOrder);
                    mo.dtjTableStatus.sendKaiPai(cardOrder);
                }, this)
            ));
            return;
        }

        var unSelectedCards = [];
        for (var j=0; j<2; j++){
            var cardNum = this.mSelectedCards[j].mCardNum;
            for (var i=0; i<4; i++){
                var card = this.mMyCards[i];
                if(card.mCardNum == cardNum){
                    var tmpCard = this.mMyCards.splice(i, 1);
                    this.mMyCards.push(tmpCard[0]);
                    break;
                }
            }
        }

        this.mSelectedCards = [];

        unSelectedCards.push(this.mMyCards[0]);
        unSelectedCards.push(this.mMyCards[1]);

        this.mSelectedCards.push(this.mMyCards[2]);
        this.mSelectedCards.push(this.mMyCards[3]);


        // var unSelectedCards = [];
        // for (var i=0; i<4; i++){
        //     var card = this.mMyCards[i];
        //     if(card.mCardNum != this.mSelectedCards[0].mCardNum
        //     && card.mCardNum != this.mSelectedCards[1].mCardNum){
        //         unSelectedCards.push(card);
        //     }
        // }

        var cardGroup1 = mo.dtjCardHelper.getCardGroup(this.mSelectedCards[0].mCardNum, this.mSelectedCards[1].mCardNum);
        var cardGroup2 = mo.dtjCardHelper.getCardGroup(unSelectedCards[0].mCardNum, unSelectedCards[1].mCardNum);


        if(cardGroup1 < cardGroup2){
            this.mSelectedCards.push(unSelectedCards.pop());
            this.mSelectedCards.push(unSelectedCards.pop());

            unSelectedCards.push(this.mSelectedCards.shift());
            unSelectedCards.push(this.mSelectedCards.shift());
        }

        var inhandPos = mo.dtjCardHelper.getInhandCardPos(0);
        for (var i=0; i<2; i++){
            var card = this.mSelectedCards[i];
            var cardSize = mo.dtjCardHelper.getInhandCardSize();

            var targetX1 = inhandPos.x + cardSize.height / 2;
            var targetY1 = inhandPos.y - cardSize.width / 2 + cardSize.width * i;

            var otherCardSize = mo.dtjCardHelper.getCardSize();

            var targetX2 = inhandPos.x + otherCardSize.height * 0.5;
            var targetY2 = inhandPos.y + otherCardSize.width * (i - 0.5);

            card.runAction(cc.sequence(
                cc.moveTo(0.2, cc.p(targetX1, targetY1)),
                cc.rotateTo(0.2, 90),
                cc.callFunc(function () {
                    this.setDtjCardRotation(90);
                }, card),
                cc.delayTime(1.0),
                cc.spawn(cc.scaleTo(0.02, mo.dtjCardHelper.cardScale), cc.moveTo(0.02, targetX2, targetY2))
            ));
        }

        for (var i=0; i<2; i++){
            var card = unSelectedCards[i];
            var cardSize = mo.dtjCardHelper.getInhandCardSize();

            var targetX = inhandPos.x - 1.5 * cardSize.width + cardSize.width * i;
            var targetY = inhandPos.y;

            var otherCardSize = mo.dtjCardHelper.getCardSize();
            var targetX2 = inhandPos.x + otherCardSize.width * (i - 1.5);
            var targetY2 = inhandPos.y;

            card.runAction(cc.sequence(
                cc.moveTo(0.2, cc.p(targetX, targetY)),
                cc.delayTime(1.2),
                cc.spawn(cc.scaleTo(0.02, mo.dtjCardHelper.cardScale), cc.moveTo(0.02, targetX2, targetY2))
            ));
        }

        //前2张小，后2张大
        var cardOrder = [this.mSelectedCards[0].mCardNum, this.mSelectedCards[1].mCardNum,unSelectedCards[0].mCardNum, unSelectedCards[1].mCardNum];
        mo.dtjTable.mDtjTableUILayer.showCardType(0, cardOrder);

        mo.dtjTableStatus.sendKaiPai(cardOrder);

        this.mCardTypeSpr1.setVisible(false);
        this.mCardTypeSpr2.setVisible(false);
    },

});